##IO distance need abut or at least 0.6um
##digital functional IO need to keep 10um to power/ground/corner/POC/clamp IO
##IVDDFS: core VDD IO
##IVSSFS: core VSS IO
##IVDDIOFS: IO power
##IVSSIOFS: IO ground
##IPOC: Power On Control cell,This library is designed to support power-on-control functions and each digital domain must include one 
##IFILLER0FS : Cell width is 0.1 um 
##IFILLER1FS : Cell width is 1 um
##IFILLER5FS : Cell width is 5 um
##IFILLER10FS : Cell width is 10 um
##ICORNERFS: corner cell for digital core
#
set height 90.5
set llx [lindex [dbget top.fplan.box] 0 0]
set lly [lindex [dbget top.fplan.box] 0 1]
set urx [lindex [dbget top.fplan.box] 0 2]
set ury [lindex [dbget top.fplan.box] 0 3]



set height_c [expr $die_height - 2*$height]
set width_c [expr $die_width - 2*$height]

set left_s 121
set bottom_s 105
set right_s 121
set top_s 113

set left_e [expr $left_s - 60]
set bottom_e [expr $bottom_s - 60]
set right_e [expr $right_s - 60]
set top_e [expr $top_s - 60]

set left_o [expr $left_e/2]
set bottom_o [expr $bottom_e/2]
set right_o [expr $right_e/2]
set top_o [expr $top_e/2]

addInst -physical -status fixed -cell ICORNER -inst corner_left_bottom -loc {0 0} -ori R0
addInst -physical -status fixed -cell ICORNER -inst corner_left_top -loc "$llx [expr $ury - $height]" -ori MX
addInst -physical -status fixed -cell ICORNER -inst corner_right_bottom -loc "[expr $urx - $height] $lly" -ori R90
addInst -physical -status fixed -cell ICORNER -inst corner_rigth_top -loc "[expr $urx - $height] [expr $ury -$height]" -ori R180



# left
addInst -physical -status fixed -cell IVDDIO -inst VDDIO_0 -loc "$llx [expr $lly + 90.5 + 6*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVSSIO -inst VSSIO_0 -loc "$llx [expr $lly + 90.5 + 7*$left_s + $left_o]" -ori R270
#addInst -physical -status fixed -cell IVDDFS -inst VDDC_0 -loc "$llx [expr $lly + 3*91.7]" -ori R270
#addInst -physical -status fixed -cell IVSSFS -inst VSSC_0 -loc "$llx [expr $lly + 4*91.7]" -ori R270

placeInstance gpio_0__inout_gpio_i 0 [expr $lly + 90.5 + 0*$left_s + $left_o] -fixed R270
placeInstance gpio_1__inout_gpio_i 0 [expr $lly + 90.5 + 1*$left_s + $left_o] -fixed R270
placeInstance gpio_2__inout_gpio_i 0 [expr $lly + 90.5 + 2*$left_s + $left_o] -fixed R270
placeInstance gpio_3__inout_gpio_i 0 [expr $lly + 90.5 + 3*$left_s + $left_o] -fixed R270
placeInstance gpio_4__inout_gpio_i 0 [expr $lly + 90.5 + 4*$left_s + $left_o] -fixed R270
placeInstance gpio_5__inout_gpio_i 0 [expr $lly + 90.5 + 5*$left_s + $left_o] -fixed R270
placeInstance gpio_6__inout_gpio_i 0 [expr $lly + 90.5 + 10*$left_s + $left_o] -fixed R270
placeInstance gpio_7__inout_gpio_i 0 [expr $lly + 90.5 + 11*$left_s + $left_o] -fixed R270
placeInstance gpio_8__inout_gpio_i 0 [expr $lly + 90.5 + 12*$left_s + $left_o] -fixed R270
placeInstance gpio_9__inout_gpio_i 0 [expr $lly + 90.5 + 13*$left_s + $left_o] -fixed R270
placeInstance gpio_10__inout_gpio_i 0 [expr $lly + 90.5 + 14*$left_s + $left_o] -fixed R270

#addInst -physical -status fixed -cell IVDDIOFS -inst VDDIO_1 -loc "$llx [expr $lly + 3000]" -ori R270
#addInst -physical -status fixed -cell IVSSIOFS -inst VSSIO_1 -loc "$llx [expr $lly + 3400]" -ori R270
addInst -physical -status fixed -cell IVDD -inst VDDC_0 -loc "$llx [expr $lly + 90.5 + 8*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVSS -inst VSSC_0 -loc "$llx [expr $lly + 90.5 + 9*$left_s + $left_o]" -ori R270



# bottom
addInst -physical -status fixed -cell IVDDIO -inst VDDIO_1 -loc "[expr $llx + 90.5 + $bottom_o] $lly" -ori R0
addInst -physical -status fixed -cell IVSSIO -inst VSSIO_1 -loc "[expr $llx + 90.5 + $bottom_s + $bottom_o] $lly" -ori R0
#addInst -physical -status fixed -cell IVDDFS -inst VDDC_3 -loc "[expr $llx + 600] $lly" -ori R0
#addInst -physical -status fixed -cell IVSSFS -inst VSSC_3 -loc "[expr $llx + 800] $lly" -ori R0

placeInstance gpio_11__inout_gpio_i [expr $lly + 90.5 + 2*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_12__inout_gpio_i [expr $lly + 90.5 + 3*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_13__inout_gpio_i [expr $lly + 90.5 + 4*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_14__inout_gpio_i [expr $lly + 90.5 + 5*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_15__inout_gpio_i [expr $lly + 90.5 + 6*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_16__inout_gpio_i [expr $lly + 90.5 + 7*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_17__inout_gpio_i [expr $lly + 90.5 + 8*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_18__inout_gpio_i [expr $lly + 90.5 + 9*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_19__inout_gpio_i [expr $lly + 90.5 + 10*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_20__inout_gpio_i [expr $lly + 90.5 + 11*$bottom_s + $bottom_o]  $lly -fixed R0

#addInst -physical -status fixed -cell IVDDIOFS -inst VDDIO_4 -loc "[expr $llx + 3400] $lly" -ori R0
#addInst -physical -status fixed -cell IVSSIOFS -inst VSSIO_4 -loc "[expr $llx + 3800] $lly" -ori R0
addInst -physical -status fixed -cell IVDD -inst VDDC_1 -loc "[expr $llx + 90.5 + 12*$bottom_s + $bottom_o] $lly" -ori R0
addInst -physical -status fixed -cell IVSS -inst VSSC_1 -loc "[expr $llx + 90.5 + 13*$bottom_s + $bottom_o] $lly" -ori R0


# right
addInst -physical -status fixed -cell IVDDIO -inst VDDIO_2 -loc "[expr $die_width - $height] [expr $lly + 90.5 + 6*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVSSIO -inst VSSIO_2 -loc "[expr $die_width - $height] [expr $lly + 90.5 + 7*$right_s + $right_o]" -ori R90
#addInst -physical -status fixed -cell IVDDFS -inst VDDC_7 -loc "11811.2 [expr $lly + 1000]" -ori R90
#addInst -physical -status fixed -cell IVSSFS -inst VSSC_7 -loc "11811.2 [expr $lly + 1100]" -ori R90

placeInstance gpio_21__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 0*$right_s + $right_o]" -fixed R90
placeInstance gpio_22__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 1*$right_s + $right_o]" -fixed R90
placeInstance gpio_23__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 2*$right_s + $right_o]" -fixed R90
placeInstance gpio_24__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 3*$right_s + $right_o]" -fixed R90
placeInstance gpio_25__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 4*$right_s + $right_o]" -fixed R90
placeInstance gpio_26__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 5*$right_s + $right_o]" -fixed R90
placeInstance gpio_27__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 10*$right_s + $right_o]" -fixed R90
placeInstance gpio_28__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 11*$right_s + $right_o]" -fixed R90
placeInstance gpio_29__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 12*$right_s + $right_o]" -fixed R90
placeInstance gpio_30__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 13*$right_s + $right_o]" -fixed R90
placeInstance gpio_31__inout_gpio_i "[expr $die_width - $height] [expr $lly + 90.5 + 14*$right_s + $right_o]" -fixed R90

#addInst -physical -status fixed -cell IVDDIOFS -inst VDDIO_8 -loc "1285 [expr $lly + 3000]" -ori R90
#addInst -physical -status fixed -cell IVSSIOFS -inst VSSIO_8 -loc "1285 [expr $lly + 3400]" -ori R90
addInst -physical -status fixed -cell IVDD -inst VDDC_2 -loc "[expr $die_width - $height] [expr $lly + 90.5 + 8*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVSS -inst VSSC_2 -loc "[expr $die_width - $height] [expr $lly + 90.5 + 9*$right_s + $right_o]" -ori R90


# top
addInst -physical -status fixed -cell IVDDIO -inst VDDIO_3 -loc "[expr $llx + 90.5 + $top_o] [expr $ury - $height]" -ori R180
addInst -physical -status fixed -cell IVSSIO -inst VSSIO_3 -loc "[expr $llx + 90.5 + $top_s + $top_o] [expr $ury - $height]" -ori R180
#addInst -physical -status fixed -cell IVDDFS -inst VDDC_10 -loc "[expr $llx + 10000] [expr $ury - $height]" -ori R180
#addInst -physical -status fixed -cell IVSSFS -inst VSSC_10 -loc "[expr $llx + 9500] [expr $ury - $height]" -ori R180

placeInstance input_clk "[expr $llx +  90.5 + 2*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance input_rstn "[expr $llx + 90.5 + 3*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance input_init_enable "[expr $llx + 90.5 + 4*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance input_rx_pin "[expr $llx + 90.5 + 5*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance input_spi_miso "[expr $llx + 90.5 + 6*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_tx_pin "[expr $llx + 90.5 + 7*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_spi_mosi "[expr $llx + 90.5 + 8*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_spi_ss "[expr $llx + 90.5 + 9*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_spi_clk "[expr $llx + 90.5 + 10*$top_s + $top_o] [expr $ury - $height]" -fixed R180

#addInst -physical -status fixed -cell IVDDIOFS -inst VDDIO_11 -loc "[expr $llx + 7500] [expr $ury - $height]" -ori R180
#addInst -physical -status fixed -cell IVSSIOFS -inst VSSIO_11 -loc "[expr $llx + 7400] [expr $ury - $height]" -ori R180
addInst -physical -status fixed -cell IVDD -inst VDDC_3 -loc "[expr $llx + 90.5 + 11*$top_s + $top_o] [expr $ury - $height]" -ori R180
addInst -physical -status fixed -cell IVSS -inst VSSC_3 -loc "[expr $llx + 90.5 + 12*$top_s + $top_o] [expr $ury - $height]" -ori R180


#deleteIoFiller
addIoFiller -cell IFILLER10 -prefix IO_FILLER 
addIoFiller -cell IFILLER5 -prefix IO_FILLER 
addIoFiller -cell IFILLER1 -prefix IO_FILLER 
addIoFiller -cell IFILLER0 -prefix IO_FILLER -fillAnyGap
fixAllIos
#attachIOBuffer

###
# power ring
#setAddRingMode -ring_target core_ring -extend_over_row 1 -ignore_rows 0 -avoid_short 1 -skip_crossing_trunks none -stacked_via_top_layer ME7 -stacked_via_bottom_layer ME6 -via_using_exact_crossover_size 1 -orthogonal_only true -skip_via_on_pin {  standardcell } -skip_via_on_wire_shape {  noshape }  
# addRing -nets {VDD VDD  VSS VSS} -type core_rings -follow core -layer {top ME6 bottom ME6 left ME7 right ME7} -width {top 3 bottom 3 left 3 right 3} -spacing {top 2 bottom 2 left 2 right 2} -offset {top 2 bottom 2 left 2 right 2} -center 1 -extend_corner {} -threshold 0 -jog_distance 0 -snap_wire_center_to_grid None -use_interleaving_wire_group 1



